﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace TestWebApplication.CommerceTestDataProvider
{
    public static class TestUtility
    {
        public static string DataContractToString(this object obj)
        {
            return DataContractToString(obj, obj.GetType());
        }

        public static string DataContractToString(this object obj, Type type)
        {
            if (obj == null)
            {
                return "NULL";
            }

            var dcs = new DataContractSerializer(type);

            using (var sw = new StringWriter())
            using (var xw = new XmlTextWriter(sw))
            {
                dcs.WriteObject(xw, obj);
                return sw.ToString();
            }
        }
    }
}
